package cn.edu.cdtu.healthclient.activity;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.cdtu.healthclient.connect.ConnectForHealthFile;
import cn.edu.cdtu.healthclient.domain.HealthFileBody;
import cn.edu.cdtu.healthclient.service.HealthService;
import cn.edu.cdtu.healthclient.service.HealthService.HealthBinder;
import cn.edu.cdtu.healthclient.task.ActivityTaskQueue;
import cn.edu.scu.healthclient.activity.R;

public class HealthFileBodyActivity extends Activity {

	/*
	 * Activity任务队列。利用线程管理Activity。 
	 */
	private ActivityTaskQueue taskQueue;
	public ActivityTaskQueue getTaskQueue(){
		return taskQueue;
	}
	/*
	 * binder联系Activity与和其绑定的Service。负责他们之间的通信。
	 */
	private HealthBinder binder;
	public HealthBinder getBinder(){
		return binder;
	}
	/*
	 * 登录模块和Service服务之间的连接设置。
	 * 
	 */
	private ServiceConnection serviceConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			binder = null;
			taskQueue.shutdownTaskQueue();
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			binder = (HealthBinder)service;
			if(!taskQueue.isRunning()){
				taskQueue.start();
			}
		}
	};
	/*
	 * 实例化一个Intent对象，该对象负责启动Service后台服务。
	 */
	private final Intent serviceIntent = new Intent(HealthService.ACTION);
	
	private TextView vitaminA;
	private TextView vitaminB;
	private TextView vitaminC;
	private TextView vitaminE;
	private TextView lecithin;
	private TextView calcium;
	private TextView aminoAcid;
	private TextView TraceElements;
	private TextView bodyAdvice;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		startService(serviceIntent);
    	taskQueue = new ActivityTaskQueue();

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.health_file_body_test);
		taskQueue.addTask(new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				HealthFileBodyAsync bodyAsync = new HealthFileBodyAsync();
				bodyAsync.execute();
			}
		}));
	}
	/* (non-Javadoc)
	 * <p>Title: onStart</p> 
	 * <p>Description: </p>  
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		bindService(serviceIntent, 
        		serviceConnection, Service.BIND_AUTO_CREATE);
		super.onStart();
	}

	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		unbindService(serviceConnection);
	}
	
	public void initTextView(){
		vitaminA = (TextView)findViewById(R.id.vitaminA);
		vitaminB = (TextView)findViewById(R.id.vitaminB);
		vitaminC = (TextView)findViewById(R.id.vitaminC);
		vitaminE = (TextView)findViewById(R.id.vitaminE);
		lecithin = (TextView)findViewById(R.id.lecithin);
		calcium = (TextView)findViewById(R.id.calcium);
		aminoAcid = (TextView)findViewById(R.id.aminoAcid);
		TraceElements = (TextView)findViewById(R.id.TraceElements);
		bodyAdvice = (TextView)findViewById(R.id.bodyAdvice);
	}
	
	public void writeTextView(HealthFileBody body){
		vitaminA.setText(""+body.getVitaminA());
		vitaminB.setText(""+body.getVitaminB());
		vitaminC.setText(""+body.getVitaminC());
		vitaminE.setText(""+body.getVitaminE());
		lecithin.setText(""+body.getLecithin());
		calcium.setText(""+body.getCalcium());
		aminoAcid.setText(""+body.getAminoAcid());
		TraceElements.setText(""+body.getTraceElements());
		bodyAdvice.setText("以上的参数根据你的测试得出。\n具体的建议信息功能真在开发中！敬请期待！");
	}

	private class HealthFileBodyAsync extends AsyncTask<Integer, String, HealthFileBody>{

		@Override
		protected HealthFileBody doInBackground(Integer... arg0) {
			// TODO Auto-generated method stub
			return new ConnectForHealthFile().getHealthFileBody(
					binder.getHttpClient());
		}

		@Override
		protected void onPostExecute(HealthFileBody body) {
			// TODO Auto-generated method stub
			super.onPostExecute(body);
			if(body!=null){
				initTextView();
				writeTextView(body);	
			}else{
				Toast.makeText(HealthFileBodyActivity.this,
						"抱歉，你并没进行过身体测试", Toast.LENGTH_LONG).show();
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

	}
}
